<?php
class Task extends CI_Model{
    
	function _construct(){
		parent::_construct();
	}
	
	function create_task($project_id, $sprint_id, $assigned_to_id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed) {
		$this->load->helper('date');
		
		$data = array(
			'project_id'			=> $project_id,
			'sprint_id'				=> $sprint_id,
			'assigned_to_id'		=> $assigned_to_id,
		   	'subject' 				=> $subject,
		   	'description'			=> $description,
		   	'priority' 				=> $priority,
			'creation_date' 		=> date('Y-m-d h:m:s', now()), 
			'status' 				=> $status,
			'estimated_hours' 		=> $estimated_hours,
			'worked_hours' 			=> $worked_hours,
			'percentaje_completed' 	=> $percentaje_completed
		);
		
		$this->db->insert('tasks', $data);
	}
	
	function delete_task($id) {
		
		$this->db->delete('tasks', array('id' => $id)); 
	}
	
	function update_task($id, $project_id, $sprint_id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed) {
		$data = array(
			'project_id'			=> $project_id,
			'sprint_id'				=> $sprint_id,
		   	'subject' 				=> $subject,
		   	'description'			=> $description,
		   	'priority' 				=> $priority,
			'status' 				=> $status,
			'estimated_hours' 		=> $estimated_hours,
			'worked_hours' 			=> $worked_hours,
			'percentaje_completed' 	=> $percentaje_completed
		);

		$this->db->where('id', $id);
		$this->db->update('tasks', $data); 
	}
	
	function get_task($id) {
		$project_id = $this->session->userdata('current_project');
		
		$this->db->select('tasks.id,
					tasks.sprint_id,
					sprints.name as sprint, 
					tasks.subject,
					tasks.description, 
					tasks.priority, 
					tasks.creation_date, 
					tasks.status, 
					tasks.estimated_hours, 
					tasks.worked_hours, 
					tasks.percentaje_completed,
					tasks.assigned_to_id, 
					users.username as assigned_to');
		$this->db->from('tasks');
		$this->db->join('users', 'tasks.assigned_to_id = users.id', 'left');
		$this->db->join('sprints', 'tasks.sprint_id = sprints.id', 'left');
		$this->db->where('tasks.id', $id);
		$this->db->where('tasks.project_id', $project_id);
		
		$query = $this->db->get();
		return $query->result_array();		
	}
	
	function get_tasks($orderByField, $orderByType, $limitStart, $limit) {
		$project_id = $this->session->userdata('current_project');
		
		$this->db->order_by($orderByField, $orderByType);
		$this->db->limit($limit, $limitStart);
		$this->db->select('id, subject, priority, creation_date, status, estimated_hours, worked_hours, percentaje_completed');
		$this->db->where('tasks.project_id', $project_id);
		$query = $this->db->get('tasks');
		return $query->result_array(); 
	}
	
	function get_tasks_count() {
		
		return $this->db->count_all_results('tasks');
	}
}
?>